package cn.tedu.boot.demo.web;

import cn.tedu.boot.demo.ex.ServiceException;
import lombok.Data;

@Data
public class JsonResult<T>{
    private Integer state;//业务返回码
    private String message;//消息
    private T data;//数据

    //私有的构造方法(不能new)
    private JsonResult() { }

    public static JsonResult<Void> ok(){
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data){
        JsonResult<T> jsonResult=new JsonResult<>();
        jsonResult.state= State.OK.getValue();
        jsonResult.data=data;
        return jsonResult;
    }

    //封装后的调用封装前的
    public static JsonResult<Void> fail(ServiceException e){
        return fail(e.getState(),e.getMessage());
    }

    public static JsonResult<Void> fail(State state,String message){
        JsonResult<Void> jsonResult=new JsonResult<>();
        jsonResult.state=state.getValue();
        jsonResult.message=message;
        return jsonResult;
    }

    //枚举
    public enum State{
        OK(2000),
        ERR_BAD_PEQUEST(40000),
        ERR_CONFLICT(40900),
        ERR_INTERNAL_ERROR(50000);

        Integer  value;

        State(Integer  value) {
            this.value=value;
        }

        public Integer getValue() {
            return value;
        }

        public void setValue(Integer value) {
            this.value = value;
        }
    }


}
